package com.cognizant.ams.controller.lastatsegment;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.cognizant.ams.bean.Lastatsegment;
import com.cognizant.ams.common.util.CognizantException;
import com.cognizant.ams.common.util.CognizantResult;
import com.cognizant.ams.common.util.ExcelUtils;
import com.cognizant.ams.service.SettlementService;
import com.cognizant.ams.bean.vo.SettlementQueryVO;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author suyon
 * @date 2019-11-11 13:29
 */
@RestController
@RequestMapping("html/sys/settlement/month")
@Api(tags="月结管理接口")
public class SettlementController {

    @Autowired
    private SettlementService settlementService;

    @GetMapping("/all")
    @ApiOperation("月结列表")
    public CognizantResult findAll(SettlementQueryVO queryVo) {

        return CognizantResult.ok().put("list", settlementService.getList(queryVo));
    }

    @GetMapping("/page")
    @ApiOperation("月结列表分页")
    public CognizantResult pageQuery(SettlementQueryVO queryVo) {

        PageInfo page = settlementService.pageQuery(queryVo);
        return CognizantResult.ok().put("page", page);
    }

    @PostMapping("/import")
    @ApiOperation("月结编辑,Excel导入接口")
    public CognizantResult uploadExcel(@ApiParam(value = "月结Excel", required = true)
                                           @RequestParam("file")MultipartFile multfile) throws Exception {

        if (multfile.isEmpty()) {
            return CognizantResult.error("文件不能为空");
        }
        // 获取文件名
        String fileName = multfile.getOriginalFilename();
        // 获取文件后缀
        String prefix=fileName.substring(fileName.lastIndexOf("."));

        ImportParams params = new ImportParams();
        params.setTitleRows(1);
        params.setHeadRows(1);
        // 用uuid作为文件名，防止生成的临时文件重复
        final File excelFile = File.createTempFile(UUID.randomUUID().toString(), prefix);
        // MultipartFile to File
        multfile.transferTo(excelFile);

        List<Lastatsegment> list = ExcelImportUtil.importExcel(excelFile,Lastatsegment.class,params);

        settlementService.saveOrUpdateByExcelList(list);

        return CognizantResult.ok().put("list",list);
    }

    /**
     * 1.普通easyPOI导出
     */
    @GetMapping("/export")
    @ApiOperation("月结列表导出Excel")
    public void export(SettlementQueryVO queryVo, HttpServletResponse response) {
        List<Lastatsegment> list = settlementService.getList(queryVo);

        StringBuffer fileName = new StringBuffer();

        if (StringUtils.isNotEmpty(queryVo.getStartTime()) && StringUtils.isNotEmpty(queryVo.getEndTime())) {
            fileName.append(queryVo.getStartTime())
                    .append("-")
                    .append(queryVo.getEndTime())
                    .append(".xls");
        }else{
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            fileName.append("All")
                    .append(format.format(new Date()))
                    .append(".xls");
        }



        ExcelUtils.exportExcel(list, "预编辑数据,编辑后重新导入才可以覆盖", "sheet1", Lastatsegment.class, fileName.toString(), response);

    }


}
